var util = require("../../../libs/util.js");
import initCalendar, {
  getSelectedDay,
  disableDay,
  setTodoLabels,
  clearTodoLabels,
  jump
} from '../../../components/calendar/main.js';

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    date: util.dateObjectformat(new Date, 'yyyy-MM'),
    data: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    wx.getSystemInfo({
      success: e => {
        console.log(e)
        app.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        app.globalData.Custom = custom;
        app.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })
    that.getData()
  },
  reportWarehouse: function () {
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  reportVehicle: function () {
    wx.navigateTo({
      url: '/pages/schedule/delivery_vehicle?type=report'
    })
  },
  reportDriver: function () {
    wx.navigateTo({
      url: '/pages/fleet/vehicleBindDriver?type=report'
    })
  },
  exportShipper: function () {
    var that = this;
    wx.showLoading({
      title: '导出中...',
    });
    util.getData("/atapi/report/business/exportShipper", {
      CO_ID: app.globalData.CO_ID,
      date: that.data.date
    }, function (res, error) {
      console.log(res.data)
      wx.hideLoading();
      if (res.data.success) {
        wx.downloadFile({
          url: res.data.data,
          success(res) {
            wx.hideLoading();
            const filePath = res.tempFilePath
            wx.openDocument({
              filePath: filePath,
              success: function (res) {
                console.log('打开文档成功')
              }
            })
          }
        });
        console.log(res.data.data)
      }
    });
  },
  getData: function () {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    try {
      if (that.data.calendar) {
        console.log(that.data.date)
      }
    } catch (e) { }
    util.getData("/atapi/report/business/month_carrier", {
      CO_ID: app.globalData.CO_ID,
      date: that.data.date + "-01",
      warehouseId: that.data.warehouseId,
      vehicle: that.data.vehicle,
      driver: that.data.driver
    }, function (res, error) {
      console.log(res.data)
      if (res.data.success) {
        that.setData({
          hasData: true,
          data: res.data.data,
          total_count: res.data.total_count,
          total_pay: res.data.total_pay,
          total_income: res.data.total_income
        })
      } else {
        that.setData({
          hasData: false,
          data: [],
          total_count: 0,
          total_pay: 0,
          total_income: 0
        })
      }
      if (!that.data.calendar) {
        that.setCalendar();
      } else {
        clearTodoLabels();
        console.log(util.dateformat(that.data.date, 'M'))
        setTodoLabels({
          pos: 'bottom',
          dotColor: '#40',
          circle: false,
          days: that.data.data
        });
        jump(Number(util.dateformat(that.data.date, 'yyyy')), Number(util.dateformat(that.data.date, 'M')), Number(util.dateformat(that.data.date, 'd')));
        wx.hideLoading();
      }
    });
  },
  setCalendar: function () {
    var that = this;
    initCalendar({
      //defaultDay: that.data.date,
      afterTapDay: (currentSelect, allSelectedDays) => {
        var date = currentSelect.year + '-' + currentSelect.month + '-' + currentSelect.day;
        console.log(date);
        if (currentSelect.hasTodo) {
          console.log(currentSelect.hasTodo);
          wx.navigateTo({
            url: '/pages/schedule/delivery_list?carrier=true&date=' + date + '&warehouse=' + (that.data.warehouseId || '') + '&warehouseName=' + (that.data.warehouse || '') + '&vehicle=' + (that.data.vehicle || '') + '&plate=' + (that.data.plate || '') + '&driver=' + (that.data.driver || '') + '&driverName=' + (that.data.driverName || '')
          })
        }
      },
      afterCalendarRender(ctx) {
        var data = that.data.data;
        console.log(data)
        // 异步请求
        setTimeout(() => {
          setTodoLabels({
            pos: 'bottom',
            dotColor: '#40',
            circle: false,
            days: data
          });
          disableDay(1);
        }, 1000);
        wx.hideLoading();
      },
      whenChangeMonth(current, next) {
        // console.log(next);
        that.setData({
          date: next.year + '-' + next.month + '-1'
        })
        that.getData()
      }
    });
  },
  DateChange(e) {
    var that = this;
    var date = e.detail.value
    this.setData({
      date: date
    });
    that.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.getData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
    wx.getStorage({
      key: 'vehicle',
      success: function (res) {
        if (res.data.plate) {
          that.setData({
            plate: res.data.plate,
            vehicle: res.data.value
          });
          that.getData();
        } else {
          that.setData({
            plate: '全部车辆',
            vehicle: ''
          });
          that.getData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'vehicle',
          success: function (res) {
          }
        });
      }
    });
    wx.getStorage({
      key: 'driver',
      success: function (res) {
        if (res.data.name) {
          that.setData({
            driverName: res.data.name,
            driver: res.data.value
          });
          that.getData();
        } else {
          that.setData({
            driverName: '全部司机',
            driver: ''
          });
          that.getData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'driver',
          success: function (res) {
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})